﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SanQiOther.Models;
using System.Data.Entity;

namespace SanQiOther.Controllers
{
    public class BusSetController : Controller
    {
        private RewardDbContext db = new RewardDbContext();
        // GET: BusSet
        public ActionResult Index()
        {
            List<SanQiOther.Models.dict_dept> dept = db.dict_dept.Where(r => r.isUsing == true).ToList<SanQiOther.Models.dict_dept>();
            SanQiOther.Models.dict_dept s = new dict_dept();
            s.id = 0;
            s.mc = "全部";
            dept.Add(s);
            ViewData["DeptList"] = new SelectList(dept, "Id", "mc", 1);


            var member = db.v_member.Where(r => r.car != 0).OrderBy(r => r.car).ThenByDescending(r => r.islz).ThenByDescending(r => r.isls);
            return View(member.ToList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            int deptid = Convert.ToInt16(form["DeptList"]);
            string username = form["MemberName"];

            List<SanQiOther.Models.dict_dept> dept = db.dict_dept.Where(r => r.isUsing == true).ToList<SanQiOther.Models.dict_dept>();
            SanQiOther.Models.dict_dept s = new dict_dept();
            s.id = 0;
            s.mc = "全部";
            dept.Add(s);
            ViewData["DeptList"] = new SelectList(dept, "Id", "mc", deptid);
            ViewData["MemberName"] = username;
            if (deptid != 0)
            {
                var member = db.v_member.Where(r => r.dept == deptid && r.name.Contains(username));
                return View("Index", member);
            }
            else
            {
                var member = db.v_member.Where(r => r.name.Contains(username));
                return View("Index", member);
            }
        }

        public ActionResult Detail(int id)
        {
            v_member member = db.v_member.FirstOrDefault(r => r.id == id);
            List<SanQiOther.Models.dict_car> dict_car = db.dict_car.Where(r => r.IsUsing == true).ToList<SanQiOther.Models.dict_car>();
            SanQiOther.Models.dict_car s = new dict_car();
            s.id = 0;
            s.mc = "无";
            dict_car.Add(s);
            ViewData["CarList"] = new SelectList(dict_car, "Id", "mc", member.car);

            return View(member);
        }

        [HttpPost]
        public ActionResult Detail(member Member, FormCollection form)
        {
            int car = Convert.ToInt16(form["CarList"]);
            SanQiOther.Models.member Member_New = new member();
            Member_New = db.member.Find(Member.id);
            if (ModelState.IsValid)
            {
                db.Entry(Member_New).State = EntityState.Modified;
                Member_New.car = car;
                db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View();
        }

    }
}